package com.heer.gymnasium;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;


import com.heer.gymnasium.api.ApiService;
import com.heer.gymnasium.api.Http;
import com.heer.gymnasium.api.SignHelper;
import com.heer.gymnasium.api.constants.IConstants;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.util.Session;
/*import com.tencent.bugly.crashreport.CrashReport;*/

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by heer on 16/6/5.
 */
public class AppHelper extends MultiDexApplication {
    static Context _context;
    static Resources _resource;
    static ApiService apiService;


    @Override
    public void onCreate() {
        super.onCreate();
        _context = getApplicationContext();
        _resource = _context.getResources();
        apiService = Http.getService();
        ButterKnife.setDebug(true);


        Context context = getApplicationContext();
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
       /* CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));*/
        // 初始化Bugly
      /*  CrashReport.initCrashReport(context, "5ddc33dc41", true, strategy);*/
        //TODO 暂时写死Token  待登陆接口写完后放开
        if (!TextUtils.isEmpty(Session.getSessionKey())) {
            SignHelper.initToken(Session.getSessionKey());
        }
//        SignHelper.initToken(IConstants.TOKEN);
        JPushInterface.init(context);
        JPushInterface.setDebugMode(true);
    }

    public static synchronized AppHelper context() {
        return (AppHelper) _context;
    }

    public ApiService apiService() {
        return apiService;
    }

    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }

    public static List<Activity> activityList;

    public static void addActivity(Activity activity) {
        if (activityList == null) {
            activityList = new ArrayList<>();
        }

        if (!(activity instanceof LoginActivity)) {
            activityList.add(0, activity);
        }
    }

    public static void clearActivity() {
        if (activityList != null) {
            for (Activity activity : activityList) {
                if (!activity.isDestroyed()) {
                    activity.finish();
                }
            }
            activityList.clear();
        }
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }
}
